package com.hdax.gulimall.ware.service.impl;

import com.hdax.common.constant.WareConstan;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.ware.dao.PurchaseDetailDao;
import com.hdax.gulimall.ware.entity.PurchaseDetailEntity;
import com.hdax.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> purchaseDetailEntityQueryWrapper = new QueryWrapper<>();
        /**
         * status:0//状态
         * wareId:1//仓库ID
         *
         */
        String key= (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            purchaseDetailEntityQueryWrapper.and(item->{
                item.eq("purchase_id",key).or().like("sku_id",key);
            });
        }
        String status= (String) params.get("status");
        if(!StringUtils.isEmpty(status)){
            purchaseDetailEntityQueryWrapper.eq("status",status);
        }
        String wareId= (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            purchaseDetailEntityQueryWrapper.eq("ware_id",wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                purchaseDetailEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据采购单的id来修改采购需求的状态
     * @param id
     * @return
     */
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> purchase_id = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        List<PurchaseDetailEntity> collect = purchase_id.stream().map(item -> {
            PurchaseDetailEntity purchaseDetail = new PurchaseDetailEntity();
            purchaseDetail.setId(item.getId());
            purchaseDetail.setStatus(WareConstan.PurchaseDetailStatusEnum.BUYING.getCode());
            return purchaseDetail;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
        return collect;
    }

}